<?php
require_once ("util.php");
require_once ("sql.php");
require_once ("filedocument.php");

class ManageFileDocuments {
	public function addDocument($fileName,$size,$url,$userId=null,$unitId=null) {
		$util = new DBUtil ();
		$conn = $util->getConnection ();
		$sql = SQLQuery::FILEDOCUMENT_ADD;
		$stmt = $conn->prepare ( $sql );
		$stmt->bind_param ( 'sdsdd', $fileName,$size,$url,$userId,$unitId);
		$status = $stmt->execute ();
		$stmt->close ();
		$conn->close ();
		return $status;
	}
	public function updateDocument($id,$fileName,$size,$uploadedOn,$url,$userId=null,$unitId=null) {
		$util = new DBUtil ();
		$conn = $util->getConnection ();
		
		// Delete document
		$sql = SQLQuery::FILEDOCUMENT_UPDATE;
		$stmt = $conn->prepare ( $sql );
		$stmt->bind_param ( 'ssssddd', $fileName,$size,$uploadedOn,$url,$userId,$unitId,$id );
		$status = $stmt->execute ();
		$stmt->close ();
		$conn->close ();
		return $status;
	}
	public function deleteDocument($documentId) {
		$util = new DBUtil ();
		$conn = $util->getConnection ();
		
		// Delete document
		$sql = SQLQuery::FILEDOCUMENT_DELETE;
		$stmt = $conn->prepare ( $sql );
		$stmt->bind_param ( 'd', $documentId );
		$status = $stmt->execute ();
		$stmt->close ();
		$conn->close ();
		return $status;
	}
	public function listDocuments($userId=null) {
		$sql = "";
		$util = new DBUtil ();
		$conn = $util->getConnection ();
		
		if ($userId !=null) {
			$sql = SQLQuery::FILEDOCUMENT_LIST;
			$stmt = $conn->prepare ( $sql );
			$stmt->bind_param ( 'd', $userId );
		} else {
			$sql = SQLQuery::FILEDOCUMENT_LIST_ALL;
			$stmt = $conn->prepare ( $sql );
		}
		$status = $stmt->execute ();
		$documents = array ();
		$result = $stmt->get_result ();
		if ($result->num_rows > 0) {
			while ( $row = $result->fetch_array () ) {
				$document = new FileDocument ();
				$document->setId ( $row ["ID"] );
				$document->setUserName ( $row ["USERNAME"] );
				$document->setUnit ( $row ["UNIT"] );
				$document->setDocumentName ( $row ["FILE_NAME"] );
				$document->setSize ( $row ["SIZE"] );
				$document->setUploadedOn ( $row ["UPLOAD_DATE"] );
				$document->setUrl ( $row ["URL"] );
				array_push ( $documents, $document );
			}
		}
		$conn->close ();
		return $documents;
	}
	public function findDocument($id) {
		$sql = SQLQuery::FILEDOCUMENT_FIND;
		$util = new DBUtil ();
		$conn = $util->getConnection ();
		$stmt = $conn->prepare ( $sql );
		$status = $stmt->execute ();
		$document = new FileDocument ();
		$result = $stmt->get_result ();
		if ($result->num_rows > 0) {
			while ( $row = $result->fetch_array () ) {
				$document->setId ( $row ["ID"] );
				$document->setUserName ( $row ["USERNAME"] );
				$document->setUnit ( $row ["UNIT"] );
				$document->setDocumentName ( $row ["FILE_NAME"] );
				$document->setSize ( $row ["SIZE"] );
				$document->setUploadedOn ( $row ["UPLOAD_DATE"] );
				$document->setUrl ( $row ["URL"] );
			}
		}
		$conn->close ();
		return $document;
	}
}
?>